import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';


import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../../../public/serve/hints/hints.service';
import { InitService } from '../../../../../public/serve/init/init.service';
import { RemoveTableService } from '../../../../../public/serve/remove-table/remove-table.service';
import { PaginationComponent } from '../../../../../public/pagination/pagination.component';
import { Service } from '../../../../../server/server';

import { appProductUrl } from '../../../../product.url';
import { product, content,exportRefreshTable } from '../../../../product.data';
import { ActivatedRoute, Params } from '@angular/router';

declare var $: any;

@Component({
    selector: 'app-product-indexing-child-list-product',
    templateUrl: './app-product-indexing-child-list-product.component.html',
    styleUrls: ['./app-product-indexing-child-list-product.component.css']
})
export class ProductIndexingChildListProductComponent implements OnInit {
    taskId: string = '';
    queryCriteria: string = '';

    total: number = 0;
    naturalNum: number = 0;
    list: Array<object>;

    selectedData: Array<any> = [];

    isSelectAll: boolean = false;
    isChose: boolean = false;
    taskType: any = '0';

    @ViewChild('pagination') pagination: PaginationComponent;

    constructor(
        private request: CudTableService,
        private server: Service,
        private hintsService: HintsService,
        private removeTableService: RemoveTableService,
        private activatedRoute: ActivatedRoute,
    ) { }


    ngOnInit() {
        this.activatedRoute.queryParams.subscribe((p: Params) => {
            this.taskType = String(p['taskType']);
        })
    }
    getSubTaskUnAssignedProduct(taskId) {
        this.isChose = this.isSelectAll = false;
        if (taskId) {
            this.taskId = taskId;
        }
        let params = this.getSubTaskUnAssignedProductParams();
        this.request.postRequest(this.server.baseUrl + appProductUrl.SubTaskUnAssignedProduct, params).then((res: any) => {
            if (res.result_code == '0000') {
                this.list = res.data;
                this.total = res.length;
                this.naturalNum = product.pagination.naturalNum;
                this.isSelectAllInCurrent();
            }else {
                this.hintsService.messages(res.message, res.result_code);
            }
        }).catch((err: any) => {
            //this.hintsService.messages('error', 'error');
            return;
        });
    }

    //是否选中当前页所有数据
    isSelectAllInCurrent() {
        let index = 0;
        for (let item of this.selectedData) {
            for (let child of this.list) {
                if (child['productId'] === item) {
                    child['isChecked'] = true;
                    index++;
                }
            }
        }
        if (index === this.list.length) {
            this.isChose = this.isSelectAll = true;
        }
    }

    getSubTaskUnAssignedProductParams() {
        return {
            token: product.retrieval.token,
            pageNo: product.pagination.pageNumber,
            pageSize: product.pagination.pageSize,
            taskId: this.taskId,
            productType: this.taskType,
            queryCriteria:this.queryCriteria
        }
    }


    /**
     * 单选
     * @param bool
     * @param child
     * @param length
     */
    _chose(bool, child, length) {
        for (let item of this.list) {
            if (item['productId'] === child) {
                item['isChecked'] = bool;
            }
        }

        if (bool) {
            this.selectedData.push(child);
        } else {
            var index = this.selectedData.indexOf(child);
            if (index > -1) {
                this.selectedData.splice(index, 1);
            }
        }

        this.isSelectAllInCurrent();
    }

    /**
     * 全选
     * @param bool
     * @param tabVal
     */
    _selectAll(bool, list) {
        for (let item of this.list) {
            if (!bool) {
                item['isChecked'] = false;
                var index = this.selectedData.indexOf(item['productId']);
                if (index > -1) {
                    this.selectedData.splice(index, 1);
                }
            }

            if (bool) {
                item['isChecked'] = true;
                var index = this.selectedData.indexOf(item['productId']);
                if (index <= -1) {
                    this.selectedData.push(item['productId']);
                }
            }
        }
        this.isSelectAll = this.isChose = bool;
    }

    /**
     * 重置表格复选框、选中的ID
     */
    public clearData() {
        this.isChose = this.isSelectAll = false;
        this.selectedData = [];
        this.pagination.currentPage = 1;
        this.pagination.pageSize = 20;
        product.pagination.pageNumber = 1;
        product.pagination.pageSize = 20;
        product.pagination.naturalNum = 0;
    }

    /**
     * 关闭窗口
     */
    public dismissModal() {
        let params = this.getAddTaskProductParams();
        params.productId = this.selectedData;
        this.addTaskProduct(params);
        exportRefreshTable.emit(true);
        this.clearData();
        $('#childTaskProductList').modal('hide');
    }

    /**
     * 新增任务产品(内容)
     * TODO 接口还没做好
     * @param params  
     */
    addTaskProduct(params) {
        this.request.postRequest(this.server.baseUrl + appProductUrl.addTaskProduct, params).then((res: any) => {
            if (res.result_code === '0000') {
                exportRefreshTable.emit(true);
            }
            this.hintsService.messages(res.message, res.result_code);
            return;
        }).catch((err: any) => {
            this.hintsService.messages('error', 'error');
            return;
        });
    }

    getAddTaskProductParams() {
        return {
            token: content.retrieval.token,
            productId: [],
            taskId: this.taskId,
            updateBy: this.server.getuserName()
        }
    }

    /**
      * 分页
      * @param pagination 
      */
    _pagination(pagination: Array<any>) {
        product.pagination.pageNumber = pagination[0].pageNumber;
        product.pagination.pageSize = pagination[0].pageSize;
        product.pagination.naturalNum = (product.pagination.pageNumber - 1) * product.pagination.pageSize;
        this.getSubTaskUnAssignedProduct('');
    }

    /**
     * 搜索
     * @param val
     */
    _retrieval(val: string) {
        this.queryCriteria = val;
        this.getSubTaskUnAssignedProduct('');
    }
}

